﻿Public Class SP_Nhap
    Dim nhapDT As NhapXuatDT
    Dim nhapBL As NhapXuatBL
    Dim nctDT As NhapChiTietDT
    Dim nctBL As NhapChiTietBL
    Dim checkclass As New CheckClass

    Private Sub LoadCBox()
        Dim nccBL As New NhaCungCapBL
        Dim spBL As New SanPhamBL
        Dim list_ncc As List(Of NhaCungCapDT)
        Dim list_sp As List(Of SanPhamDT)
        list_ncc = nccBL.GetData("")
        list_sp = spBL.GetData("")

        With cbNCC
            .DataSource = list_ncc
            .DisplayMember = "TenNCC"
            .ValueMember = "MaNCC"
            .SelectedIndex = 0
        End With
        With cbLoai
            .DataSource = list_sp
            .DisplayMember = "TenLoai"
            .ValueMember = "MaLoai"
            .SelectedIndex = 0
        End With
    End Sub

    Private Sub SP_Nhap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadCBox()
    End Sub

    Private Sub nhap_chitiet(ByVal manhap As String)
        If IsNumeric(txtSL.Text) = True Then
            'Them chi tiet
            nctBL = New NhapChiTietBL
            Dim stt As Integer = nctBL.GetSTT(manhap)
            If cbNCC.Enabled = True Then
                nctDT = New NhapChiTietDT(manhap, stt, cbLoai.SelectedValue, cbNCC.SelectedValue.ToString(), txtSL.Text, txtGhiChu.Text)
            Else
                nctDT = New NhapChiTietDT(manhap, stt, cbLoai.SelectedValue, txtSL.Text, txtGhiChu.Text)
            End If
            nctBL.InNhapCT(nctDT)
        Else
            txtSL.BackColor = Color.MistyRose
            txtSL.Text = ""
            Return
        End If
    End Sub

    Private Sub nhap_sp()
        If txtSL.Text = "" Or cbLoai.Text = "Chưa chọn loại" Then
            MessageBox.Show("Nhập sản phẩm phải có loại và số lượng sản phẩm")
        Else
            Dim ma As String
            Dim i As Integer
            nhapBL = New NhapXuatBL
            'kiem tra ma nhap cua today co chua ? neu co roi thi nhap vao chi tiet truc tiep
            If checkclass.IsExist(" SanPham_Nhap", " where ngayNhap= '" + Date.Today + "'") = True Then
                ma = nhapBL.GetMa(" SanPham_Nhap", "ngayNhap", Date.Today, "MaNhap")
                'Them vao chitiet
                nhap_chitiet(ma)
            Else
                i = nhapBL.GetID(" SanPham_Nhap_ChiTiet")
                ma = "N" + i.ToString("00000")
                nhapDT = New NhapXuatDT(ma, Date.Today, Main.NVid)
                nhapBL.InNhap(nhapDT)
                'Them vao chi tiet
                nhap_chitiet(ma)
            End If
        End If
    End Sub

    Public Sub Nhap(ByVal sender As System.Object, ByVal e As System.EventArgs)
        nhap_sp()
        txtSL.Text = ""
        txtGhiChu.Text = ""
    End Sub

    Private Sub cbLoai_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLoai.SelectedIndexChanged
        If cbLoai.SelectedIndex <> 0 Then
            lbDVT.Visible = True
            Dim dt As New DataTable
            Dim cn As New DatabaseConnect
            dt = cn.GetData("select dvt.donvitinh from SanPham sp,SanPham_DonViTinh dvt where sp.donvitinh = dvt.donvitinhID and sp.MaLoai =" & cbLoai.SelectedValue & "")
            lbDVT.Text = dt.Rows(0)("donvitinh").ToString()
        End If
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Dim ncc As New NCC_Nhap()
        ncc.flagIsUp = True
        ncc.ShowDialog()
        LoadCBox()
        Dim nccBL As New NhaCungCapBL
        Dim i As String = nccBL.GetInserted()
        cbNCC.SelectedValue = i
    End Sub

    Private Sub txtSL_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSL.TextChanged
        If txtSL.Text <> "" Then
            If IsNumeric(txtSL.Text) = False Then
                txtSL.BackColor = Color.Chocolate
                txtSL.ForeColor = Color.White
            Else
                txtSL.BackColor = Color.White
                txtSL.ForeColor = Color.Black
            End If
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            cbNCC.Enabled = False
        Else
            cbNCC.Enabled = True
        End If
    End Sub
End Class
